package alone.redis.queue.redisqueue.service;

import alone.redis.queue.redisqueue.entity.UserInfo;
import alone.redis.queue.redisqueue.mapper.SignInMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户注册类
 * 基于redis队列实现用户注册成功后发短信和发邮件的操作，降低耦合度。
 */
@Service
@Slf4j
public class SignInService {

    @Value("${redis.queue.channelName}")
    private String channelName;

    @Resource
    private SignInMapper signInMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户注册并将其信息发布以备订阅者使用
     * @param name userName
     * @param sex userSex
     * @return result
     */
    public String insertData(String name,String sex){
        //执行注册  插入数据库
        UserInfo userInfo = new UserInfo(name,sex);
        signInMapper.userSignIn(userInfo);
        //注册成功后将用户信息放入redis队列
        stringRedisTemplate.convertAndSend(channelName,userInfo.toString());
        log.info("{} sign in success and publish to redis's {} success",userInfo.toString(),channelName);
        return "success";
    }

}
